package com.sgb.goods.view.activity.inquiry;

import android.content.Intent;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.PopupWindow;

import androidx.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.databinding.GoodsActivityNewPurchasesBinding;
import com.sgb.goods.entity.AdmixtureFaceEntity;
import com.sgb.goods.entity.AttributeViewEntity;
import com.sgb.goods.entity.BrandEntity;
import com.sgb.goods.entity.CommonStAttrEntity;
import com.sgb.goods.entity.ConcreteEntity;
import com.sgb.goods.entity.GoodsAttrEntity;
import com.sgb.goods.entity.GoodsItemEntity;
import com.sgb.goods.entity.GoodsUnitEntity;
import com.sgb.goods.entity.MenuEntity1;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.base.BaseActivity;
import com.sgb.goods.view.adapter.publish.pop.BrandPopWindow;
import com.sgb.goods.view.adapter.publish.pop.GoodsTypePopWindow;
import com.sgb.goods.viewmodel.inquiry.NewPurchasesModel;
import com.sgb.goods.widget.pop.GoodsAttributeViewWindows;
import com.sgb.goods.widget.pop.StAttributeViewWindows;
import com.swgk.core.util.MToast;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：询价-新增采购品
 *
 * @author qiangHeNooo
 * <p>
 * 时间：2021.11.01
 */
@Route(path = Constants.ARouter.AR_INQUIRY_NEW_PURCHASES)
public class NewPurchasesActivity extends BaseActivity<GoodsActivityNewPurchasesBinding, NewPurchasesModel> {

    private static final String TAG = "NewPurchasesActivity";
    /**
     * 商品分类选择弹窗
     */
    private GoodsTypePopWindow goodsNamePopWindow;

    /**
     * 商品属性选择弹窗
     */
    private GoodsAttributeViewWindows attributeViewWindows;

    /**
     * 品牌选择弹窗
     */
    private BrandPopWindow brandPopWindow;
    /**
     * 商砼属性选择弹窗
     */
    private StAttributeViewWindows stAttributeViewWindows;
    /**
     * 商品属性集合
     */
    private List<AttributeViewEntity> attributeViewEntityList;
    /**
     * 商砼属性集合
     */
    private List<CommonStAttrEntity> commonStAttrEntityList;

    /**
     * 商品分类列表
     */
    private List<MenuEntity1<MenuEntity1<MenuEntity1>>> goodsTypeEntityList;

    /**
     * 新三级类目id
     */
    private String cid3 = "";

    /**
     * 旧三级类目id
     */
    private String lastCid3 = "";

    /**
     * 单位列表
     */
    private List<GoodsUnitEntity> unitEntityList;
    /**
     * 商砼type
     */
    private final static String GOODS_TYPE_ST = "商砼";
    /**
     * 选取商品是否为商砼
     */
    private boolean isST = false;
    /**
     * 单个商品sku实体类
     */
    private GoodsItemEntity goodsItem;
    private int goodsUnitPosition;
    /**
     * 商砼
     */
    private ConcreteEntity concrete;
    /**
     * 品牌列表
     */
    private List<BrandEntity> brandEntityList;
    private boolean isEdit;
    private boolean isAdding;
    private String mustName = "";

    @Override
    protected int getLayoutResId() {
        return R.layout.goods_activity_new_purchases;
    }

    @Override
    protected void init() {
        initView();
        loadData();
    }

    private void initView() {
        mDataBinding.head.title.setText("新增采购品");
        mDataBinding.head.right.setText("确定");
        mDataBinding.goodsAmount.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
    }

    private void loadData() {
        this.isEdit = getIntent().getBooleanExtra("GOOD_EDIT", false);
        this.isAdding = getIntent().getBooleanExtra("GOOD_ADD", false);
        Serializable serializable = getIntent().getSerializableExtra("GOOD_ENTITY");
        obtainType(serializable);
        initPopWindow();
    }

    @Override
    protected void initListener() {
        initBackListener();
        initConfirmListener();
        initGoodsNameListener();
        initGoodsAttributeListener();
        initGoodsBrandListener();
        initGoodsUnitListener();
    }

    /**
     * 商品单位监听
     */
    private void initGoodsUnitListener() {
        mDataBinding.goodsUnit.setOnWholeItemClickListener(view -> {
            if (!isST) {
                if (unitEntityList != null) {
                    mDataBinding.goodsAmount.clearFocus();
                    showUnit();
                } else {
                    mViewModel.getUnitList("");
                }
            }
        });
    }

    /**
     * 商品品名监听
     */
    private void initGoodsBrandListener() {
        mDataBinding.goodsBrand.setOnWholeItemClickListener(view -> {
            if (!cid3.equals(lastCid3)) {
                mViewModel.getBrand(cid3, "");
                return;
            }
            if (brandEntityList != null) {
                showBrandPop();
            } else {
                mViewModel.getBrand(cid3, "");
            }
        });
    }

    /**
     * 商品（商砼）属性信息监听
     */
    private void initGoodsAttributeListener() {
        mDataBinding.goodsAttribute.setOnWholeItemClickListener(view -> {
            if (TextUtils.isEmpty(mDataBinding.goodsName.getContent())) {
                MToast.showToast(this, "请先选择商品");
                return;
            }
            if (isST) {
                if (commonStAttrEntityList != null && commonStAttrEntityList.size() != 0) {
                    showStAttributePop(false);
                } else {
                    mViewModel.getStAttribute();
                }
            } else {
                if (!cid3.equals(lastCid3)) {
                    mViewModel.getAttribute(cid3, false);
                    return;
                }
                if (attributeViewEntityList != null && attributeViewEntityList.size() > 0) {
                    showAttributePop(false);
                } else {
                    if (!TextUtils.isEmpty(cid3)) {
                        mViewModel.getAttribute(cid3, false);
                        lastCid3 = cid3;
                    } else {
                        if (goodsItem != null && !"".equals(goodsItem.getTcategory())) {
                            mViewModel.getAttribute(goodsItem.getTcategory(), false);
                            lastCid3 = cid3;
                        }
                    }
                }
            }
        });
    }

    /**
     * 商品品名监听
     */
    private void initGoodsNameListener() {
        mDataBinding.goodsName.setOnWholeItemClickListener(view -> {
            if (goodsTypeEntityList != null) {
                showGoodsTypePop();
            } else {
                mViewModel.getGoodsCategory();
            }
        });
    }

    /**
     * 确认点击事件监听
     */
    private void initConfirmListener() {
        mDataBinding.head.right.setOnClickListener(v -> {
            if (!isInputValid()) {
                return;
            }
            if (!isAdding) {
                if (isST) {
                    this.concrete.setNum(getGoodsAmount());
                    ARouter.getInstance().build(Constants.ARouter.AR_INQUIRY_PUBLISHING).withSerializable("GOOD_ITEM", this.concrete).navigation();
                } else {
                    this.goodsItem.setNum(getGoodsAmount());
                    ARouter.getInstance().build(Constants.ARouter.AR_INQUIRY_PUBLISHING).withSerializable("GOOD_ITEM", this.goodsItem).navigation();
                }
            } else {
                Intent intent = new Intent();
                if (isST) {
                    this.concrete.setNum(getGoodsAmount());
                    intent.putExtra("GOOD_ITEM", this.concrete);
                } else {
                    this.goodsItem.setNum(getGoodsAmount());
                    intent.putExtra("GOOD_ITEM", this.goodsItem);
                }
                if (isEdit) {
                    setResult(0x11, intent);
                } else {
                    setResult(0x12, intent);
                }
            }
            finish();
        });
    }

    private void initBackListener() {
        mDataBinding.head.ivBack.setOnClickListener(view -> finish());
    }

    @Override
    protected void initViewModel() {
        mViewModel = createViewModel(NewPurchasesModel.class, BR.viewModel);
    }

    @Override
    protected void initObserve() {
        mViewModel.menuEntity.observe(NewPurchasesActivity.this, this::showGoodsTypePop);
        mViewModel.isAttributeVisibility.observe(NewPurchasesActivity.this, this::setAttributeVisibility);
        mViewModel.attributeViewEntity.observe(NewPurchasesActivity.this, this::showAttributePop);
        mViewModel.commonStAttrEntity.observe(NewPurchasesActivity.this, this::showStAttributePop);
        mViewModel.brandEntity.observe(NewPurchasesActivity.this, this::showBrandPop);
        mViewModel.unitEntity.observe(NewPurchasesActivity.this, this::showUnit);
    }

    /**
     * 显示商品单位的弹窗
     */
    public void showUnit(List<GoodsUnitEntity> data) {
        this.unitEntityList = data;
        showUnit();
    }

    private void showUnit() {
        OptionsPickerView pickerView = new OptionsPickerBuilder(NewPurchasesActivity.this, (options1, options2, options3, v) -> {
            String content = unitEntityList.get(options1).getName();
            setGoodsUnit(content);
            goodsUnitPosition = options1;
        }).setLineSpacingMultiplier(2.0f)
                .setCancelColor(0xFF969696)
                .setTitleColor(0xFF969696)
                .setSubCalSize(15)
                .setTitleSize(15)
                .build();

        pickerView.setPicker(unitEntityList);
        pickerView.setSelectOptions(goodsUnitPosition);
        pickerView.setTitleText("选择单位");
        pickerView.show();
    }

    /**
     * 显示商品品牌弹窗
     */
    public void showBrandPop(List<BrandEntity> data) {
        this.brandEntityList = data;
        showBrandPop();
    }

    private void showBrandPop() {
        if (!brandPopWindow.isShowing()) {
            brandPopWindow.showDownPopwindow(mDataBinding.grayView, true);
        }
        brandPopWindow.setData(brandEntityList, cid3);
    }

    /**
     * 设置商品的单位
     *
     * @param content 选框选取的品牌
     */
    private void setGoodsUnit(String content) {
        mDataBinding.goodsUnit.setContent(content);
        this.goodsItem.setUnit(content);
    }

    private void showStAttributePop(boolean isUpdate) {
        if (!stAttributeViewWindows.isShowing()) {
            stAttributeViewWindows.showDownPopwindow(mDataBinding.grayView, true);
        }
        stAttributeViewWindows.setData(commonStAttrEntityList, isUpdate);
    }

    /**
     * 显示商砼属性popWindow
     */
    public void showStAttributePop(List<CommonStAttrEntity> data) {
        this.commonStAttrEntityList = data;
        showStAttributePop(true);
    }

    /**
     * 新增采购品类型
     */
    private void obtainType(Serializable serializable) {
        if (serializable == null) {
            this.concrete = new ConcreteEntity();
            this.goodsItem = new GoodsItemEntity();
            return;
        }
        if (serializable instanceof ConcreteEntity) {
            loadData((ConcreteEntity) serializable);
            this.goodsItem = new GoodsItemEntity();
        } else if (serializable instanceof GoodsItemEntity) {
            loadData((GoodsItemEntity) serializable);
            this.concrete = new ConcreteEntity();
        }

    }

    /**
     * 商砼
     */
    private void loadData(ConcreteEntity concrete) {
        if (concrete == null) {
            this.concrete = new ConcreteEntity();
        } else {
            this.concrete = concrete;
            if (!TextUtils.isEmpty(concrete.getCartTitle())) {
                setGoodsName(concrete.getCartTitle());
            } else {
                String goodName = concrete.getFcatName()
                        + "-" + concrete.getScatName()
                        + "-" + concrete.getTcatName();
                setGoodsName(goodName);
            }
            setAttribute(getConcreteAttribute(concrete));
            setGoodsAmount(concrete.getNum());
            setBrandVisibility(false);
            setAttributeVisibility(true);
            setConcreteGoodsUnit("立方米");
        }
        this.isST = true;
    }

    /**
     * 获取商砼属性值
     */
    private String getConcreteAttribute(ConcreteEntity concrete) {
        StringBuilder stringBuilder = new StringBuilder();
        if (!TextUtils.isEmpty(concrete.getStrongLevel())) {
            stringBuilder.append(concrete.getStrongLevel()).append(" ");
        }
        if (!TextUtils.isEmpty(concrete.getDeliveryMethod())) {
            stringBuilder.append("1".equals(concrete.getDeliveryMethod()) ? "自卸" : "泵送").append(" ");
        }
        if (!TextUtils.isEmpty(concrete.getConfig())) {
            stringBuilder.append("1".equals(concrete.getConfig()) ? "一级配" : "二级配").append(" ");
        }

        List<AdmixtureFaceEntity> admixtureFactList = concrete.getAdmixtureFactList();
        if (admixtureFactList != null && admixtureFactList.size() != 0) {
            for (AdmixtureFaceEntity admixtureFace : admixtureFactList) {
                if (!TextUtils.isEmpty(admixtureFace.getAdmixtureName())) {
                    stringBuilder.append(admixtureFace.getAdmixtureName()).append(" ");
                }
            }
        }
        return stringBuilder.toString();
    }

    /**
     * 设置商品属性信息
     */
    private void setAttribute(String content) {
        mDataBinding.goodsAttribute.setContent(content);

    }

    /**
     * 普通商品
     */
    private void loadData(GoodsItemEntity goodsItem) {
        if (goodsItem == null) {
            this.goodsItem = new GoodsItemEntity();
        } else {
            this.goodsItem = goodsItem;
            if (goodsItem.getFcatName() != null) {
                String goodName = goodsItem.getFcatName()
                        + "-" + goodsItem.getScatName()
                        + "-" + goodsItem.getTcatName();
                setGoodsName(goodName);
                String mAttribute = getGoodsItemAttribute(goodsItem);
                if ("".equals(mAttribute)) {
                    setAttributeVisibility(false);
                } else {
                    setAttribute(mAttribute);
                }
                setGoodsAmount(goodsItem.getNum());
                setBrandName(goodsItem.getBrandName());
                setGoodsUnit(goodsItem.getUnit());
            }
        }
        this.isST = false;
    }

    private String getGoodsItemAttribute(GoodsItemEntity goodsItem) {
        List<GoodsAttrEntity> goodsItemAttrList = goodsItem.getAttrList();
        StringBuilder stringBuilder = new StringBuilder();
        if (goodsItemAttrList != null && goodsItemAttrList.size() != 0) {
            for (GoodsAttrEntity goodsAttr : goodsItemAttrList) {
                stringBuilder.append(goodsAttr.getParamValName()).append(" ");
            }
        }
        return stringBuilder.toString();
    }

    private void initPopWindow() {
        if (goodsNamePopWindow == null) {
            goodsNamePopWindow = new GoodsTypePopWindow(this);
        }
        goodsNamePopWindow.setBgView(mDataBinding.grayView);
        goodsNamePopWindow.setOnResultClick((key1, key2, key3) -> {
            // 一级（左侧列表一级数据），obj1.id 代表 cid1
            MenuEntity1 obj1 = (MenuEntity1) key1;
            // 二级（右侧列表一级数据），obj2.id 代表 cid2
            MenuEntity1 obj2 = (MenuEntity1) key2;
            // 三级（右侧列表二级数据），obj3.id 代表 cid3
            MenuEntity1 obj3 = (MenuEntity1) key3;
            cid3 = obj3.id;
            resetGoodsAttribute();
            setGoodsName(obj1.name
                    + "-" + obj2.name
                    + "-" + obj3.name);
            if (!TextUtils.equals(GOODS_TYPE_ST, obj1.name)) {
                isST = false;
                mViewModel.getAttribute(cid3, true);
                setBrandVisibility(true);
                buildGoodsItem(obj1, obj2, obj3);
            } else {//商砼
                isST = true;
                buildConcrete(obj1, obj2, obj3);
                setBrandVisibility(false);
                setConcreteGoodsUnit("立方米");
                setAttributeVisibility(true);
            }
        });

        if (attributeViewWindows == null) {
            attributeViewWindows = new GoodsAttributeViewWindows(this);
        }
        attributeViewWindows.setBgView(mDataBinding.grayView);
        attributeViewWindows.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        attributeViewWindows.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        attributeViewWindows.setOnResultClick((key1, key2, key3) -> buildAttrs(key1));

        if (stAttributeViewWindows == null) {
            stAttributeViewWindows = new StAttributeViewWindows(this);
        }
        stAttributeViewWindows.setBgView(mDataBinding.grayView);
        stAttributeViewWindows.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        stAttributeViewWindows.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        stAttributeViewWindows.setOnResultClick((key1, key2, key3) -> buildStAttrs(key1, key2));
        if (brandPopWindow == null) {
            brandPopWindow = new BrandPopWindow(NewPurchasesActivity.this);
        }
        brandPopWindow.setBgView(mDataBinding.grayView);
        brandPopWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        brandPopWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        brandPopWindow.setOnResultClick((key1, key2, key3) -> {
            setBrandName((String) key2);
            goodsItem.setBrandId((String) key1);
        });

        brandPopWindow.inputTextInterface(value -> {
            hideInput();
            mViewModel.getBrand(cid3, value);
        });
    }

    protected void hideInput() {
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        View v = getWindow().peekDecorView();
        if (null != v) {
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
    }

    /**
     * 构建商品属性
     *
     * @param key1 商品属性
     */
    private void buildAttrs(Object key1) {
        List<GoodsAttrEntity> goodsAttrList = new ArrayList<>();
        StringBuilder stringBuffer = new StringBuilder();
        try {
            if (key1 instanceof String[]) {
                String[] checkedAttributes = (String[]) key1;
                if (checkedAttributes.length > 0) {
                    for (String attributeString : checkedAttributes) {
                        if (!TextUtils.isEmpty(attributeString)) {
                            String[] splitAttributes = attributeString.split("&");
                            GoodsAttrEntity goodsAttr = new GoodsAttrEntity();
                            goodsAttr.setParamId(splitAttributes[0]);
                            goodsAttr.setParamName(splitAttributes[1]);
                            goodsAttr.setParamValId(splitAttributes[2]);
                            goodsAttr.setParamValName(splitAttributes[3]);
                            goodsAttrList.add(goodsAttr);
                            stringBuffer.append(splitAttributes[3]).append(" ");
                        }
                    }
                    setAttrList(goodsAttrList);
                    mDataBinding.goodsAttribute.setContent(stringBuffer.toString());
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    /**
     * 构建商砼属性
     *
     * @param key1 加强剂属性（多选）
     * @param key2 其他属性（单选）
     */
    private void buildStAttrs(Object key1, Object key2) {
        StringBuilder stringBuilder = new StringBuilder();
        if (key1 instanceof Map && key2 instanceof Map) {
            HashMap admixtureAttrMap = (HashMap) key1;
            HashMap otherAttrMap = (HashMap) key2;

            for (Map.Entry<String, CommonStAttrEntity.ChildListBean> otherAttr : (Iterable<Map.Entry<String, CommonStAttrEntity.ChildListBean>>) otherAttrMap.entrySet()) {
                CommonStAttrEntity.ChildListBean childListBean = otherAttr.getValue();
                buildConcreteAttribute(otherAttr.getKey(), null, childListBean, stringBuilder);
            }

            for (Map.Entry<String, List<CommonStAttrEntity.ChildListBean>> entry : (Iterable<Map.Entry<String, List<CommonStAttrEntity.ChildListBean>>>) admixtureAttrMap.entrySet()) {
                List<CommonStAttrEntity.ChildListBean> attributeList = entry.getValue();
                if (attributeList != null && attributeList.size() > 0) {
                    buildConcreteAttribute(entry.getKey(), attributeList, null, stringBuilder);
                }
            }
        }
    }

    /**
     * 构建商砼的属性信息
     *
     * @param key           属性标题
     * @param admixtureList 加强剂属性
     * @param otherAttr     其他属性
     * @param stringBuilder 拼接显示属性的builder
     */
    private void buildConcreteAttribute(String key, List<CommonStAttrEntity.ChildListBean> admixtureList, CommonStAttrEntity.ChildListBean otherAttr, StringBuilder stringBuilder) {
        if ("strength".equals(key)) {
            stringBuilder.append(otherAttr.getName()).append(" ");
            concrete.setStrongLevel(otherAttr.getName());
        }
        if ("config".equals(key)) {
            stringBuilder.append(otherAttr.getName()).append(" ");
            concrete.setConfig(TextUtils.equals(otherAttr.getName(), "一级配") ? "1" : "2");
        }
        if ("deliveryMethod".equals(key)) {
            stringBuilder.append(otherAttr.getName()).append(" ");
            concrete.setDeliveryMethod(TextUtils.equals(otherAttr.getName(), "自卸") ? "1" : "2");
        }
        if ("admixture".equals(key)) {
            List<AdmixtureFaceEntity> list = new ArrayList<>();
            for (CommonStAttrEntity.ChildListBean admixtureAttr : admixtureList) {
                if (admixtureAttr != null) {
                    AdmixtureFaceEntity admixtureFace = new AdmixtureFaceEntity();
                    admixtureFace.setAdmixtureName(admixtureAttr.getName());
                    admixtureFace.setSpec(String.valueOf(admixtureAttr.getSpec()));
                    stringBuilder.append(admixtureAttr.getName()).append(" ");
                    list.add(admixtureFace);
                }
            }
            concrete.setAdmixtureFactList(list);
        }
        mDataBinding.goodsAttribute.setContent(stringBuilder.toString());
    }

    /**
     * 设置商品品牌
     *
     * @param content 选框选取的品牌
     */
    private void setBrandName(String content) {
        mDataBinding.goodsBrand.setContent(content);
        goodsItem.setBrandName(content);
    }

    /**
     * 重新选取商品后重置各个属性
     */
    private void resetGoodsAttribute() {
        mDataBinding.goodsName.setContent("");
        mDataBinding.goodsAttribute.setContent("");
        mDataBinding.goodsBrand.setContent("");
        mDataBinding.goodsAmount.setContent("");
        mDataBinding.goodsUnit.setContent("");

    }

    /**
     * 构建商品基本属性
     */
    private void buildGoodsItem(MenuEntity1 menuEntity1, MenuEntity1 menuEntity2, MenuEntity1 menuEntity3) {
        goodsItem.setFcatName(menuEntity1.name);
        goodsItem.setFcategory(menuEntity1.id);
        goodsItem.setScatName(menuEntity2.name);
        goodsItem.setScategory(menuEntity2.id);
        goodsItem.setTcatName(menuEntity3.name);
        goodsItem.setTcategory(menuEntity3.id);
    }

    /**
     * 构建商砼基本属性
     */
    private void buildConcrete(MenuEntity1 menuEntity1, MenuEntity1 menuEntity2, MenuEntity1 menuEntity3) {
        concrete.setFcatName(menuEntity1.name);
        concrete.setFcategory(menuEntity1.id);
        concrete.setScatName(menuEntity2.name);
        concrete.setScategory(menuEntity2.id);
        concrete.setTcatName(menuEntity3.name);
        concrete.setTcategory(menuEntity3.id);
    }

    /**
     * 设置商品的名字
     *
     * @param content 选框选取后商品的名字
     */
    private void setGoodsName(String content) {
        mDataBinding.goodsName.setContent(content);
    }

    /**
     * 设置商品属性条目的可见性
     *
     * @param visibility 可见性
     */
    public void setAttributeVisibility(boolean visibility) {
        mDataBinding.goodsAttribute.setVisibility(visibility ? View.VISIBLE : View.GONE);
        if (!visibility) {
            setAttrList(new ArrayList<>());
        }
    }

    /**
     * 设置属性集合
     */
    private void setAttrList(List<GoodsAttrEntity> attrList) {
        goodsItem.setAttrList(attrList);
    }

    /**
     * 显示商品类型
     */
    private void showGoodsTypePop() {
        if (!goodsNamePopWindow.isShowing()) {
            goodsNamePopWindow.showDownPopwindow(mDataBinding.grayView, true);
        }
    }

    private void showGoodsTypePop(List<MenuEntity1<MenuEntity1<MenuEntity1>>> data) {
        this.goodsTypeEntityList = data;
        if (!goodsNamePopWindow.isShowing()) {
            goodsNamePopWindow.showDownPopwindow(mDataBinding.grayView, true);
        }
        goodsNamePopWindow.setData(goodsTypeEntityList);
    }

    /**
     * 展示商品属性弹窗
     */
    private void showAttributePop(List<AttributeViewEntity> data) {
        this.attributeViewEntityList = data;
        showAttributePop(true);
    }

    /**
     * 设置商品的采购量
     */
    private void setGoodsAmount(String amount) {
        mDataBinding.goodsAmount.setContent(amount);

    }

    /**
     * 获取商品的采购量
     */
    private String getGoodsAmount() {
        String goodsAmount = mDataBinding.goodsAmount.getContent();
        if (goodsAmount.endsWith(".")) {
            goodsAmount = goodsAmount.substring(0, goodsAmount.length() - 1);
        }
        return goodsAmount;
    }

    /**
     * 设置品牌条目的可见性（商砼 无品牌属性）
     *
     * @param visibility 可见性
     */
    private void setBrandVisibility(boolean visibility) {
        mDataBinding.goodsBrand.setVisibility(visibility ? View.VISIBLE : View.GONE);
    }

    /**
     * 设置商砼的单位
     *
     * @param content “立方米”
     */
    private void setConcreteGoodsUnit(String content) {
        mDataBinding.goodsUnit.setContent(content);
        this.concrete.setUnit(content);
    }

    private void showAttributePop(boolean isUpdate) {
        if (!attributeViewWindows.isShowing()) {
            attributeViewWindows.showDownPopwindow(mDataBinding.grayView, true);
        }
        attributeViewWindows.setData(attributeViewEntityList, isUpdate);
    }

    /**
     * 校验页面输入是否合规
     *
     * @return 是否合规
     */
    private boolean isInputValid() {
        if (TextUtils.isEmpty(mDataBinding.goodsName.getContent())) {
            MToast.showToast(NewPurchasesActivity.this, "请选择商品的品名");
            return false;
        }
        if (isST) {
            if (TextUtils.isEmpty(concrete.getStrongLevel()) && TextUtils.isEmpty(concrete.getConfig())
                    && TextUtils.isEmpty(concrete.getDeliveryMethod()) && (concrete.getAdmixtureFactList() == null || concrete.getAdmixtureFactList().size() == 0)) {
                MToast.showToast(NewPurchasesActivity.this, "请选择商品属性");
                return false;
            }
            if (TextUtils.isEmpty(concrete.getStrongLevel())) {
                MToast.showToast(NewPurchasesActivity.this, "请选择商品属性的强度等级");
                return false;
            }
            if (TextUtils.isEmpty(concrete.getConfig())) {
                MToast.showToast(NewPurchasesActivity.this, "请选择商品属性的配置类型");
                return false;
            }
            if (TextUtils.isEmpty(concrete.getDeliveryMethod())) {
                MToast.showToast(NewPurchasesActivity.this, "请选择商品属性的服务类型");
                return false;
            }
        } else {
            if (mDataBinding.goodsAttribute.getVisibility() != View.GONE) {
                if (isEdit) {
                    if (attributeViewEntityList != null && attributeViewEntityList.size() != 0) {
                        if (!isAttributeAllFilling()) {
                            MToast.showToast(NewPurchasesActivity.this, "请选择商品" + mustName + "属性");
                            return false;
                        }
                    }
                } else {
                    if (!isAttributeAllFilling()) {
                        MToast.showToast(NewPurchasesActivity.this, "请选择商品" + mustName + "属性");
                        return false;
                    }
                }
            }
        }
        if (TextUtils.isEmpty(mDataBinding.goodsUnit.getContent())) {
            MToast.showToast(NewPurchasesActivity.this, "请选择单位");
            return false;
        }
        String goodsAmount = mDataBinding.goodsAmount.getContent();
        if (TextUtils.isEmpty(goodsAmount)) {
            MToast.showToast(NewPurchasesActivity.this, "请输入商品数量");
            return false;
        }
        try {
            if (goodsAmount.endsWith(".")) {
                goodsAmount = goodsAmount.substring(0, goodsAmount.length() - 1);
            }
            double amount = Double.parseDouble(goodsAmount);
            double VALUE_MAX = 999999.99;
            if (amount <= 0.0) {
                MToast.showToast(NewPurchasesActivity.this, "请输入正确的采购数值");
                return false;
            } else if (amount > VALUE_MAX) {
                MToast.showToast(NewPurchasesActivity.this, "采购量不得超过999999.99");
                return false;
            }
        } catch (Exception exception) {
            MToast.showToast(NewPurchasesActivity.this, "请输入正确的采购数量");
            return false;
        }
        return true;
    }

    private boolean isAttributeAllFilling() {
        if (attributeViewEntityList == null || attributeViewEntityList.size() == 0) {
            return false;
        }
        for (AttributeViewEntity attributeViewEntity : attributeViewEntityList) {
            if (attributeViewEntity.getIsFill() == 1) {
                if (!isContainerValue(attributeViewEntity.getId())) {
                    mustName = attributeViewEntity.getName();
                    return false;
                }
            }
        }

        return true;
    }

    private boolean isContainerValue(String id) {
        if (this.goodsItem == null || this.goodsItem.getAttrList() == null || this.goodsItem.getAttrList().size() == 0) {
            return false;
        }
        for (GoodsAttrEntity goodsAttr : this.goodsItem.getAttrList()) {
            if (id.equals(goodsAttr.getParamId())) {
                return true;
            }
        }
        return false;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (999 == resultCode) {
            mViewModel.getBrand(cid3, "");
        }
    }

}